package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.media.dtos.WmNewsPageReqDto;
import com.heima.model.media.pojos.WmNews;
import com.heima.model.media.pojos.WmUser;
import com.heima.model.user.pojos.ApUserRealname;
import com.heima.utils.threadlocal.WmThreadLocalUtils;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmNewsService;
import org.apache.commons.lang3.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@Slf4j
public class WmNewsServiceImpl extends ServiceImpl<WmNewsMapper, WmNews> implements WmNewsService {

    @Value("${fdfs.url}")
    String fileServerUrl;

    @Override
    public ResponseResult findList(WmNewsPageReqDto dto) {

        //1.检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        dto.checkParam();

        //检查登录
        WmUser user = WmThreadLocalUtils.getUser();
        if(user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        //2.封装条件
        //2.1 分页条件
        Page<WmNews> pageRequest = new Page<>(dto.getPage(), dto.getSize());

        LambdaQueryWrapper<WmNews> lambdaQueryWrapper = Wrappers.<WmNews>lambdaQuery();

        //状态精确查询
        if(dto.getStatus() != null){
            lambdaQueryWrapper.eq(WmNews::getStatus,dto.getStatus());
        }

        //2.3 检查关键字条件
        if(StringUtils.isNotBlank(dto.getKeyWord())){
            lambdaQueryWrapper.like(WmNews::getTitle,dto.getKeyWord());
        }

        //2.4 检查频道条件
        if(null != dto.getChannelId()){
            lambdaQueryWrapper.eq(WmNews::getChannelId,dto.getChannelId());
        }

        //2.5 检查发布日期 时间条件
        if(dto.getBeginPubdate()!=null && dto.getEndPubdate()!=null){
            lambdaQueryWrapper.between(WmNews::getPublishTime,dto.getBeginPubdate(),dto.getEndPubdate());
        }

        //2.6 检查用户登录ID条件
        lambdaQueryWrapper.eq(WmNews::getUserId,user.getId());

        //2.7 按照文章发表时间排序
        lambdaQueryWrapper.orderByDesc(WmNews::getPublishTime);

        //3 执行查询 封装结果
        IPage<WmNews> pageResult = page(pageRequest, lambdaQueryWrapper);

        PageResponseResult responseResult = new PageResponseResult(dto.getPage(),dto.getSize(),pageResult.getTotal());

        responseResult.setData(pageResult.getRecords());
//        responseResult.setHost(fileServerUrl);
        return responseResult;
    }
}
